/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dsrv6.persistence.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Dsrv6.persistence.controllers.exceptions.NonexistentEntityException;
import Dsrv6.persistence.entities.InsuranceType;
import Dsrv6.persistence.entities.IdType;
import Dsrv6.persistence.entities.Gender;
import Dsrv6.persistence.entities.Cooperative;
import Dsrv6.persistence.entities.Register;

/**
 *
 * @author a381u296
 */
public class RegisterJpaController implements Serializable {

    public RegisterJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Register register) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InsuranceType instInstId = register.getInstInstId();
            if (instInstId != null) {
                instInstId = em.getReference(instInstId.getClass(), instInstId.getInstId());
                register.setInstInstId(instInstId);
            }
            IdType idtIdtId = register.getIdtIdtId();
            if (idtIdtId != null) {
                idtIdtId = em.getReference(idtIdtId.getClass(), idtIdtId.getIdtId());
                register.setIdtIdtId(idtIdtId);
            }
            Gender gndGndId = register.getGndGndId();
            if (gndGndId != null) {
                gndGndId = em.getReference(gndGndId.getClass(), gndGndId.getGndId());
                register.setGndGndId(gndGndId);
            }
            Cooperative cooCooId = register.getCooCooId();
            if (cooCooId != null) {
                cooCooId = em.getReference(cooCooId.getClass(), cooCooId.getCooId());
                register.setCooCooId(cooCooId);
            }
            em.persist(register);
            if (instInstId != null) {
                instInstId.getRegisterList().add(register);
                instInstId = em.merge(instInstId);
            }
            if (idtIdtId != null) {
                idtIdtId.getRegisterList().add(register);
                idtIdtId = em.merge(idtIdtId);
            }
            if (gndGndId != null) {
                gndGndId.getRegisterList().add(register);
                gndGndId = em.merge(gndGndId);
            }
            if (cooCooId != null) {
                cooCooId.getRegisterList().add(register);
                cooCooId = em.merge(cooCooId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Register register) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Register persistentRegister = em.find(Register.class, register.getRegId());
            InsuranceType instInstIdOld = persistentRegister.getInstInstId();
            InsuranceType instInstIdNew = register.getInstInstId();
            IdType idtIdtIdOld = persistentRegister.getIdtIdtId();
            IdType idtIdtIdNew = register.getIdtIdtId();
            Gender gndGndIdOld = persistentRegister.getGndGndId();
            Gender gndGndIdNew = register.getGndGndId();
            Cooperative cooCooIdOld = persistentRegister.getCooCooId();
            Cooperative cooCooIdNew = register.getCooCooId();
            if (instInstIdNew != null) {
                instInstIdNew = em.getReference(instInstIdNew.getClass(), instInstIdNew.getInstId());
                register.setInstInstId(instInstIdNew);
            }
            if (idtIdtIdNew != null) {
                idtIdtIdNew = em.getReference(idtIdtIdNew.getClass(), idtIdtIdNew.getIdtId());
                register.setIdtIdtId(idtIdtIdNew);
            }
            if (gndGndIdNew != null) {
                gndGndIdNew = em.getReference(gndGndIdNew.getClass(), gndGndIdNew.getGndId());
                register.setGndGndId(gndGndIdNew);
            }
            if (cooCooIdNew != null) {
                cooCooIdNew = em.getReference(cooCooIdNew.getClass(), cooCooIdNew.getCooId());
                register.setCooCooId(cooCooIdNew);
            }
            register = em.merge(register);
            if (instInstIdOld != null && !instInstIdOld.equals(instInstIdNew)) {
                instInstIdOld.getRegisterList().remove(register);
                instInstIdOld = em.merge(instInstIdOld);
            }
            if (instInstIdNew != null && !instInstIdNew.equals(instInstIdOld)) {
                instInstIdNew.getRegisterList().add(register);
                instInstIdNew = em.merge(instInstIdNew);
            }
            if (idtIdtIdOld != null && !idtIdtIdOld.equals(idtIdtIdNew)) {
                idtIdtIdOld.getRegisterList().remove(register);
                idtIdtIdOld = em.merge(idtIdtIdOld);
            }
            if (idtIdtIdNew != null && !idtIdtIdNew.equals(idtIdtIdOld)) {
                idtIdtIdNew.getRegisterList().add(register);
                idtIdtIdNew = em.merge(idtIdtIdNew);
            }
            if (gndGndIdOld != null && !gndGndIdOld.equals(gndGndIdNew)) {
                gndGndIdOld.getRegisterList().remove(register);
                gndGndIdOld = em.merge(gndGndIdOld);
            }
            if (gndGndIdNew != null && !gndGndIdNew.equals(gndGndIdOld)) {
                gndGndIdNew.getRegisterList().add(register);
                gndGndIdNew = em.merge(gndGndIdNew);
            }
            if (cooCooIdOld != null && !cooCooIdOld.equals(cooCooIdNew)) {
                cooCooIdOld.getRegisterList().remove(register);
                cooCooIdOld = em.merge(cooCooIdOld);
            }
            if (cooCooIdNew != null && !cooCooIdNew.equals(cooCooIdOld)) {
                cooCooIdNew.getRegisterList().add(register);
                cooCooIdNew = em.merge(cooCooIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = register.getRegId();
                if (findRegister(id) == null) {
                    throw new NonexistentEntityException("The register with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Register register;
            try {
                register = em.getReference(Register.class, id);
                register.getRegId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The register with id " + id + " no longer exists.", enfe);
            }
            InsuranceType instInstId = register.getInstInstId();
            if (instInstId != null) {
                instInstId.getRegisterList().remove(register);
                instInstId = em.merge(instInstId);
            }
            IdType idtIdtId = register.getIdtIdtId();
            if (idtIdtId != null) {
                idtIdtId.getRegisterList().remove(register);
                idtIdtId = em.merge(idtIdtId);
            }
            Gender gndGndId = register.getGndGndId();
            if (gndGndId != null) {
                gndGndId.getRegisterList().remove(register);
                gndGndId = em.merge(gndGndId);
            }
            Cooperative cooCooId = register.getCooCooId();
            if (cooCooId != null) {
                cooCooId.getRegisterList().remove(register);
                cooCooId = em.merge(cooCooId);
            }
            em.remove(register);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Register> findRegisterEntities() {
        return findRegisterEntities(true, -1, -1);
    }

    public List<Register> findRegisterEntities(int maxResults, int firstResult) {
        return findRegisterEntities(false, maxResults, firstResult);
    }

    private List<Register> findRegisterEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Register.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Register findRegister(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Register.class, id);
        } finally {
            em.close();
        }
    }

    public int getRegisterCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Register> rt = cq.from(Register.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
